<!DOCTYPE html>
<html lang="en">

    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <link href="css/bootstrap.min.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="css/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">

        <!-- DataTables CSS -->
        <link href="css/plugins/dataTables.bootstrap.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="css/sb-admin-2.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <?php include './script/manufacturesScript.php'; ?>


    </head>

    <body>
        <?php
        session_start();
        if (!isset($_SESSION['role'])) {
            header("Location: login.php");
        }
        ?>
        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0;">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="index.html">Xin chào <?php echo $_SESSION['name'] ?></a> <!-- get User late -->
                </div>
                <!-- /.navbar-header -->

                <ul class="nav navbar-top-links navbar-right" >

                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-user fa-fw"></i>  <i class="fa fa-caret-down"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="info.php"><i class="fa fa-user fa-fw"></i> Thông tin cá nhân</a>
                            </li>

                            <li class="divider"></li>
                            <li><a href="script/logoutScript.php"><i class="fa fa-sign-out fa-fw"></i> Đăng xuất</a>
                            </li>
                        </ul>
                        <!-- /.dropdown-user -->
                    </li>
                    <!-- /.dropdown -->
                </ul>
                <!-- /.navbar-top-links -->

                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <ul class="nav" id="side-menu">
                            <?php
                            if ($_SESSION['role'] == 1) {
                                ?>
                                <li>
                                    <a class="active" href="statistic.php"><i class="fa fa-bar-chart-o fa"></i> &nbspThống kê<span class="fa arrow"></span></a>
                                    <ul class="nav nav-second-level">
                                        <li>
                                            <a href="statistic.php">Doanh thu</a>
                                        </li>
                                    </ul>
                                </li>

                                <li>
                                    <a href="users.php"><i class="fa fa-user fa-fw"></i> Quản lí nhân viên</a>

                                    <!-- /.nav-second-level -->
                                </li>
                                <?php
                            }
                            ?>
                            <li>
                                <a href="customers.php"><i class="fa fa-wrench fa-fw"></i>&nbsp;Quản lí khách hàng</a>

                                <!-- /.nav-second-level -->
                            </li>
                            <li>
                                <a href="#"><i class="fa fa-shopping-cart fa-fw"></i> Quản lí sản phẩm<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="categories.php">Danh mục</a>
                                    </li>
                                    <li>
                                        <a href="manufacturers.php">Nhà sản xuất</a>
                                    </li>                                    
                                    <li>
                                        <a href="products.php">Sản phẩm</a>
                                    </li>
                                </ul>
                            </li>
                            <li>

                                <a href="#"><i class="fa fa-edit fa-fw"></i> Quản lí đơn hàng<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="orders_in.php">Hóa đơn nhập</a>
                                    </li>
                                    <li>
                                        <a href="orders_out.php">Hóa đơn xuất</a>
                                    </li>

                                </ul>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </div>
                    <!-- /.sidebar-collapse -->
                </div>
                <!-- /.navbar-static-side -->
            </nav>

            <!-- Page Content -->
            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <h3 class="page-header" style="color:#1c92c7">Quản lý nhà sản xuất</h3>
                    </div>

                    <!-- /.col-lg-12 -->
                </div>

                <!-- /.row -->
                <div class="row">
                    <!-- Nav tabs -->
                    <ul class="nav nav-tabs" role="tablist">

                        <li class="active"><a href="#view" role="tab" data-toggle="tab">Danh sách nhà phân phối</a></li>
                        <li ><a href="#new" role="tab" data-toggle="tab">Thêm mới nhà phân phối</a></li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active " id="view">
                            <div id="view-content">
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="panel panel-default">
                                            <!-- /.panel-heading -->
                                            <div class="panel-body">
                                                <div class="table-responsive">
                                                    <div class="panel-heading">
                                                        <div id = "message" class="alert alert-success" role="alert" style="display:none">Xóa thành công</div>
                                                    </div>
                                                    <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                                        <thead>
                                                            <tr>
                                                                <th style="text-align:center;vertical-align:middle">Nhà cung cấp</th>
                                                                <th style="text-align:center;vertical-align:middle">Danh mục</th>
                                                                <th></th>
                                                                <th></th>
                                                            </tr>
                                                        </thead>
                                                        <?php
                                                        displayBody($_SESSION['role']);
                                                        ?>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>


                        <div class="tab-pane " id="new">
                            <div id="new-content">
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="panel panel-default">
                                            <!-- /.panel-heading -->
                                            <div class="panel-body">
                                                <div id ="addnew" >
                                                    <div id = "addMess" class="alert alert-warning" role="alert" style="display: none" >Nhập tên nhà sản xuất mới , hệ thống sẽ thông báo nếu hợp lệ</div>
                                                    <form class="form-horizontal" role="form" id="formAddNew">
                                                        <div id = "m-s" class="alert alert-success" role="alert" style="display: none">Tạo mới thành công</div>
                                                        <div id = "m-e" class="alert alert-danger" role="alert" style="display: none">Không thể tạo mới</div>
                                                         </br>
                                                        <div class="form-group">
                                                            <label for="newName" class="col-sm-2 control-label" >
                                                                <i id="ok" style="color: #00cc33 ;display: none" class="fa fa-check"></i>
                                                                <i id="nok" style="color: red ;display: none"  class="fa fa-times"></i>
                                                            </label>
                                                          
                                                            <div class="col-sm-4">
                                                                <input type="text" class="form-control" id="newName" placeholder="Nhập tên nhà sản xuất">

                                                            </div>

                                                        </div>
                                                         </br>                                                   <div class="form-group" style="padding-left:150px">
                                                         <span class="label label-danger" >Danh mục sản phẩm</span>
                                                        </div>   
                                                        <div class="form-group">
                                                            
                                                              <?php
                                                                    getCheckboxCates();
                                                              ?>
                                                            
                                                        </div>   
                                                        <div class="form-group">
                                                            <div class="col-sm-offset-2 col-sm-10">
                                                                <button id ="bt" type="button" class="btn btn-primary"  style="float: right ">Cập nhật</button>
                                                                <button  id = "rs" type="reset" class="btn btn-default" style="float:right;margin-right: 10px">Reset</button>
                                                            </div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>

                    </div>

                </div>
                <!--row -->

                <!-- end row -->
                <!-- /#wrapper -->
            </div>
            <!-- Button trigger modal -->

            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">

                            <h4 class="modal-title" id="myModalLabel"><span class="label label-info" >Chỉnh sửa nhà sản xuất</span></h4>
                        </div>
                        <div class="modal-body">
                            <form class="form-horizontal" role="form">
                                <div id = "mm-s" class="alert alert-success" role="alert" style="display: none">Cập nhật thành công</div>
                                <div id = "mm-e" class="alert alert-danger" role="alert" style="display: none">Không thể cập nhật</div>
                                <div class="form-group">

                                    <label  for="modal-old" class="col-sm-2 control-label" ></label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="modal-old" disabled>
                                    </div>

                                </div>    
                                <div class="form-group">
                                    <label  for="modal-new" class="col-sm-2 control-label" ></label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="modal-new" placeholder="Nhập tên nhà sản xuất">
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">

                            <button id="btnUpdate" type="button" class="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- jQuery Version 1.11.0 -->
            <script src="js/jquery-1.11.0.js"></script>

            <!-- Bootstrap Core JavaScript -->
            <script src="js/bootstrap.min.js"></script>

            <!-- Metis Menu Plugin JavaScript -->
            <script src="js/plugins/metisMenu/metisMenu.min.js"></script>

            <!-- DataTables JavaScript -->
            <script src="js/plugins/dataTables/jquery.dataTables.js"></script>
            <script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>

            <!-- Custom Theme JavaScript -->
            <script src="js/sb-admin-2.js"></script>

            <!-- Page-Level Demo Scripts - Tables - Use for reference -->
            <script>

                $(document).ready(function() {
                    $('#dataTables-example').dataTable();
                    $("#qlsp").click();
                });

            </script>
            <script>
                $("button").click(function() {

                    if ($(this).attr("value") == "edit") {

                        var customerId = $(this).closest('tr').find('td:eq(0)').text();
                        $('#modal-old').val(customerId);

                    } else if ($(this).attr("value") == "del") {

                        var select = confirm('Bạn thực sự muốn xóa ?');
                        if (select) {
                            var id = $(this).attr("id");

                            $.ajax({
                                type: "POST",
                                url: "./script/manufacturesScript.php",
                                data: {id: id}
                            })
                                    .done(function(msg) {

                                        $('#message').slideDown('fast').delay(1500).slideUp('slow');
                                        window.location.href = 'manufacturers.php';
                                    });
                        } else {

                        }
                    }

                });
            </script>
            <script>
                $('#btnadd').click(function() {
                    if ($('#addnew').css('display') == 'none') {
                        $('#addMess').slideDown('fast').delay(3000).slideUp('slow');
                    } else {
                        $('#addMess').hide();
                    }
                    $('#addnew').slideToggle(500);
                    $('html, body').animate({
                        scrollTop: $("#bt").offset().top
                    }, 1200);



                });
            </script>
            <script>
                $("#newName").blur(function() {

                    var newManu = $("#newName").val().trim();

                    if (newManu.length > 0) {
                        $.ajax({
                            type: "POST",
                            url: "./script/manufacturesScript.php",
                            data: {new : newManu}
                        })
                                .done(function(msg) {
                                    if (msg.length == 1) {
                                        $('#nok').show();
                                        $('#ok').hide();
                                        $('#bt').attr("disabled", "true");
                                    } else if (msg.length == 2) {
                                        $('#ok').show();
                                        $('#nok').hide();
                                        $('#bt').removeAttr("disabled");
                                    }
                                });
                    }

                });
            </script>
            <script>
                $('#rs').click(function() {
                
                    $('#ok').hide();
                    $('#nok').hide();
                });
            </script>
            <script>
                $('#bt').click(function() {
                    var list = $('input:checkbox:checked');
                    var cate = "";
                    for(var i = 0 ;i<list.size();i++){
                        cate += " " + list.eq(i).val() +";";
                    }
                    
                  //  alert(cate);
                    var newManu = $("#newName").val().trim();
                    if (newManu.length > 0) {
                        $.ajax({
                            type: "POST",
                            url: "./script/manufacturesScript.php",
                            data: {addnew: newManu,category:cate}
                        })
                                .done(function(msg) {
                                  //  alert(msg);
                                    if (msg.search("Error") != -1) {
                                        $('#m-s').hide();
                                        $('#m-e').slideDown('fast').delay(3000).slideUp('slow');
                                        $('html, body').animate({
                                            scrollTop: $("#bt").offset().top
                                        }, 1200);
                                        $('#rs').click();
                                    } else {
                                        $('#m-e').hide();
                                        $('#m-s').slideDown('fast').delay(3000).slideUp('slow');
                                        $('html, body').animate({
                                            scrollTop: $("#bt").offset().top
                                        }, 1200);
                                        window.location.href = 'manufacturers.php';
                                    }
                                });

                    }
                });
            </script>
            <script>
                $('#btnUpdate').click(function() {
                    var newName = $('#modal-new').val().trim();
                    var oldName = $('#modal-old').val();

                    if (newName.length > 0) {
                        $.ajax({
                            type: "POST",
                            url: "./script/manufacturesScript.php",
                            data: {newName: newName, oldName: oldName}
                        })
                                .done(function(msg) {
                                    if (msg.search("Error") != -1) {
                                        $('#mm-s').hide();
                                        $('#mm-e').slideDown('fast').delay(3000).slideUp('slow');

                                    } else {
                                        $('#mm-e').hide();
                                        $('#mm-s').slideDown('fast').delay(3000).slideUp('slow');

                                        window.location.href = 'manufacturers.php';
                                    }
                                });
                    }
                });
            </script>
    </body>

</html>
